<?php
declare (strict_types=1);

namespace app\api\middleware;

use app\api\business\Jwt;
use think\Exception;

class Check
{
    protected $url = [
        'login',
        'captcha',
        'logout'
    ];

    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $response = $next($request);
        if (!in_array($request->pathinfo(), $this->url) && $request->pathinfo() != "captcha/" . $request->param('id') . ".html") {
            // 取出token
            $token = Jwt::getRequestToken();
            try {
                // 验证token值
                $res = Jwt::verifyjwt($token);
                cache('uid', $res->getData()['user_id']);
            } catch (Exception $exception) {
                return fail($exception->getMessage());
            }
        }
        return $response;
    }
}
